package com.slz3.business.module.goodsManagement.gooAttributeValue.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.goodsManagement.gooAttributeValue.domain.GooAttributeValue;
import com.slz3.business.module.goodsManagement.gooAttributeValue.domain.GooAttributeValueFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.List;

public class GooAttributeValueResExecutor {

    static Mono<ServerResponse> add(ServerRequest req, GooAttributeValue gooAttributeValue) {
        GooAttributeValue gooAttributeValueDBLatest = Services.gooAttributeValue.save(gooAttributeValue);
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(gooAttributeValueDBLatest).build());
    }

    /**
     * known: gooAttributeValueList is not null
     */
    static Mono<ServerResponse> delete(ServerRequest req) {
        List<GooAttributeValue> gooAttributeValueList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooAttributeValueList)) {
            List<GooAttributeValue> gooAttributeValueListNew = Services.gooAttributeValue.delete(gooAttributeValueList);
            if (VE.isNotEmpty(gooAttributeValueListNew) && gooAttributeValueListNew.size() == gooAttributeValueList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooAttributeValueListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("delete fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> modify(ServerRequest req, GooAttributeValue source) {
        GooAttributeValue target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            Steward.merge(source, target);
            GooAttributeValue gooAttributeValueDBLatest = Services.gooAttributeValue.modify(target);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(gooAttributeValueDBLatest).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> list(ServerRequest req) {
        Page<GooAttributeValue> gooAttributeValuePage = Services.gooAttributeValue.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(gooAttributeValuePage).build());
    }

    static Mono<ServerResponse> info(ServerRequest req) {
        GooAttributeValue target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> choice(ServerRequest req) {
        Page<GooAttributeValue> gooAttributeValuePage = Services.gooAttributeValue.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(gooAttributeValuePage).build());
    }

    /**
     * known: gooAttributeValueList is not null
     */
    static Mono<ServerResponse> auditSubmit(ServerRequest req) {
        List<GooAttributeValue> gooAttributeValueList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooAttributeValueList)) {
            gooAttributeValueList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.WAIT_AUDIT);
                GooAttributeValueFactory.initBaseFields(item);
            });
            List<GooAttributeValue> gooAttributeValueListNew = Services.gooAttributeValueTransactional.batchModify(gooAttributeValueList);
            if (VE.isNotEmpty(gooAttributeValueListNew) && gooAttributeValueListNew.size() == gooAttributeValueList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooAttributeValueListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditSubmit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: gooAttributeValueList is not null
     */
    static Mono<ServerResponse> audit(ServerRequest req) {
        List<GooAttributeValue> gooAttributeValueList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooAttributeValueList)) {
            gooAttributeValueList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.APPROVED);
                GooAttributeValueFactory.initBaseFields(item);
            });
            List<GooAttributeValue> gooAttributeValueListNew = Services.gooAttributeValueTransactional.batchModify(gooAttributeValueList);
            if (VE.isNotEmpty(gooAttributeValueListNew) && gooAttributeValueListNew.size() == gooAttributeValueList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooAttributeValueListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("audit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: gooAttributeValueList is not null
     */
    static Mono<ServerResponse> auditReject(ServerRequest req) {
        List<GooAttributeValue> gooAttributeValueList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooAttributeValueList)) {
            gooAttributeValueList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.REJECT);
                GooAttributeValueFactory.initBaseFields(item);
            });
            List<GooAttributeValue> gooAttributeValueListNew = Services.gooAttributeValueTransactional.batchModify(gooAttributeValueList);
            if (VE.isNotEmpty(gooAttributeValueListNew) && gooAttributeValueListNew.size() == gooAttributeValueList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooAttributeValueListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: gooAttributeValueList is not null
     */
    static Mono<ServerResponse> usingEnable(ServerRequest req) {
        List<GooAttributeValue> gooAttributeValueList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooAttributeValueList)) {
            gooAttributeValueList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.ENABLE);
                GooAttributeValueFactory.initBaseFields(item);
            });
            List<GooAttributeValue> gooAttributeValueListNew = Services.gooAttributeValueTransactional.batchModify(gooAttributeValueList);
            if (VE.isNotEmpty(gooAttributeValueListNew) && gooAttributeValueListNew.size() == gooAttributeValueList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooAttributeValueListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }


    /**
     * known: gooAttributeValueList is not null
     */
    static Mono<ServerResponse> usingDisable(ServerRequest req) {
        List<GooAttributeValue> gooAttributeValueList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(gooAttributeValueList)) {
            gooAttributeValueList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.DISABLE);
                GooAttributeValueFactory.initBaseFields(item);
            });
            List<GooAttributeValue> gooAttributeValueListNew = Services.gooAttributeValueTransactional.batchModify(gooAttributeValueList);
            if (VE.isNotEmpty(gooAttributeValueListNew) && gooAttributeValueListNew.size() == gooAttributeValueList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(gooAttributeValueListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("usingDisable fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

}
